/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.emp.solbrillante.sales.view;

import java.sql.Timestamp;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import pe.emp.solbrillante.sale.Main;
import pe.emp.solbrillante.sales.bean.PaqueteCompra;
import pe.emp.solbrillante.sales.bean.Pedido;

/**
 *
 * @author Ronald
 */
public class JPedido extends javax.swing.JDialog {
    

    /**
     * Creates new form JPedido
     */
    public JPedido(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
                 //pintamos el fondo de la ventana
        ((JFondo)jventana).setImagen("fondo_subVentanal_II.jpg");
        
        ((JFondo)jtitulo).setImagen("BarraHorizontalTitulos.png");
        ((JFondo)jsubtitulo).setImagen("BarraHorizontalTitulosBlanco.png");
        
        MostarPedidos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jventana = new JFondo();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTablePedidos = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jtitulo = new JFondo();
        jLabel1 = new javax.swing.JLabel();
        jsubtitulo = new JFondo();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Pedido a Proveedor");
        setMaximumSize(new java.awt.Dimension(850, 550));
        setMinimumSize(new java.awt.Dimension(850, 550));
        setPreferredSize(new java.awt.Dimension(850, 550));

        jventana.setMaximumSize(new java.awt.Dimension(800, 500));
        jventana.setMinimumSize(new java.awt.Dimension(800, 500));
        jventana.setPreferredSize(new java.awt.Dimension(800, 500));

        jTablePedidos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTablePedidos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTablePedidosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTablePedidos);

        jButton3.setText("Eliminar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.setText("Nuevo");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jtitulo.setMaximumSize(new java.awt.Dimension(267, 39));
        jtitulo.setMinimumSize(new java.awt.Dimension(267, 39));
        jtitulo.setPreferredSize(new java.awt.Dimension(267, 39));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Control de Pedidos");

        javax.swing.GroupLayout jtituloLayout = new javax.swing.GroupLayout(jtitulo);
        jtitulo.setLayout(jtituloLayout);
        jtituloLayout.setHorizontalGroup(
            jtituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jtituloLayout.createSequentialGroup()
                .addContainerGap(91, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(73, 73, 73))
        );
        jtituloLayout.setVerticalGroup(
            jtituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jtituloLayout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap())
        );

        jsubtitulo.setMaximumSize(new java.awt.Dimension(267, 39));
        jsubtitulo.setMinimumSize(new java.awt.Dimension(267, 39));
        jsubtitulo.setPreferredSize(new java.awt.Dimension(267, 39));

        jLabel2.setText("Pedidos a la fecha:");

        javax.swing.GroupLayout jsubtituloLayout = new javax.swing.GroupLayout(jsubtitulo);
        jsubtitulo.setLayout(jsubtituloLayout);
        jsubtituloLayout.setHorizontalGroup(
            jsubtituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jsubtituloLayout.createSequentialGroup()
                .addContainerGap(91, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(85, 85, 85))
        );
        jsubtituloLayout.setVerticalGroup(
            jsubtituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jsubtituloLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jventanaLayout = new javax.swing.GroupLayout(jventana);
        jventana.setLayout(jventanaLayout);
        jventanaLayout.setHorizontalGroup(
            jventanaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jventanaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jventanaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jventanaLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3))
                    .addGroup(jventanaLayout.createSequentialGroup()
                        .addGroup(jventanaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jsubtitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jventanaLayout.setVerticalGroup(
            jventanaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jventanaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jtitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addComponent(jsubtitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jventanaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton3))
                .addGap(73, 73, 73))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jventana, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jventana, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:        
                   JCompra jcompra;
                   jcompra = new JCompra(new VentanaPrincipalLogin(), true);
                   jcompra.setLocation(this.getLocation().x+20,this.getLocation().y+10);
                   jcompra.setPadre(this);
                   jcompra.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTablePedidosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTablePedidosMouseClicked
        // TODO add your handling code here:
        int idpedido = 0;
        String estado = null ;
        int row = jTablePedidos.rowAtPoint(evt.getPoint());
        int col = jTablePedidos.columnAtPoint(evt.getPoint());
        if (row >= 0 && col >= 0) {
            //this.jTablePedidos.getModel().getValueAt(row, col);
            idpedido = Integer.parseInt(this.jTablePedidos.getModel().getValueAt(row, 0).toString());
            estado   =  this.jTablePedidos.getModel().getValueAt(row, 6).toString() ;
            System.out.println("IDPEDIDO : " + idpedido);
            System.out.println("Estado : " + estado);
        }
       if( estado.equalsIgnoreCase("pendiente") ){ 
        JDetallePedido jdetalleproducto;
        jdetalleproducto = new JDetallePedido(new VentanaPrincipalLogin(), true, idpedido);
        //posicion de la ventana
        jdetalleproducto.setLocation(this.getLocation().x+20,this.getLocation().y+100);
        /*
         creamos un setPadre en la clase JDetallePedido para como objeto a la ventana JPedido
         * y poder usar sus metodos
         */
        jdetalleproducto.setPadre(this);
        jdetalleproducto.setVisible(true);   
       }
        
    }//GEN-LAST:event_jTablePedidosMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JPedido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JPedido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JPedido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JPedido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JPedido dialog = new JPedido(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTablePedidos;
    private javax.swing.JPanel jsubtitulo;
    private javax.swing.JPanel jtitulo;
    private javax.swing.JPanel jventana;
    // End of variables declaration//GEN-END:variables

    public void MostarPedidos() {
          //throw new UnsupportedOperationException("Not yet implemented");
          //Declaramos las clases necesarias para la conexion     
            
            DefaultTableModel modelo;                               
            
            modelo = new DefaultTableModel(){
            //boolean[] canEdit = new boolean [] { false, false,false, false, false, false , false , false};
            boolean[] canEdit = new boolean [] { false, false,false, false, false, false,false};
            @Override
                         public boolean isCellEditable(int rowIndex, int columnIndex) {
                             return canEdit [columnIndex];
                         }

            };
            
            this.jTablePedidos.setModel(modelo);            
        
            //Mostrar todos los pedidos (todos los estados)
            ArrayList<Object> t_lista_pedidos = Main.myService.queryListaPedidos();                 
    
            modelo.addColumn("Pedido");
            modelo.addColumn("Proveedor");                      
            modelo.addColumn("Tipo Pedido");                      
            modelo.addColumn("Fecha Registro");
            modelo.addColumn("Fecha Cierre");            
            modelo.addColumn("Total");
            modelo.addColumn("Estado");
            //modelo.addColumn("VerDetalle");
            //modelo.addColumn("Eliminar");
            
           System.out.println("size : " + t_lista_pedidos.size());
            for (int i = 0; i < t_lista_pedidos.size(); i++) {                                           
                modelo.addRow((Object[])t_lista_pedidos.get(i));                  
            }                           
           
        
    }
    
      public void refreshProductTable(){
          MostarPedidos();
      }
    
}
